package com.appfahrt.gui;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	private Context context;

	private String[] groups = { "Meine Fahrgesuche", "Meine Fahrten", "Historie" };

	private String[][] children = { { "Child1" }, { "Child2" }, { "Child3" },
			{ "Child4" }, { "Child5" } };

	
	public ExpandableListAdapter(Context pContext){
		this.context = pContext;
	}
	// /**
	// * A general add method, that allows you to add a Vehicle to this list
	// *
	// * Depending on if the category opf the vehicle is present or not,
	// * the corresponding item will either be added to an existing group if it
	// * exists, else the group will be created and then the item will be added
	// * @param vehicle
	// */
	// public void addItem(Vehicle vehicle) {
	// if (!groups.contains(vehicle.getGroup())) {
	// groups.add(vehicle.getGroup());
	// }
	// int index = groups.indexOf(vehicle.getGroup());
	// if (children.size() < index + 1) {
	// children.add(new ArrayList<Vehicle>());
	// }
	// children.get(index).add(vehicle);
	// }

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return children[groupPosition][childPosition];
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	// Return a child view. You can load your custom layout here.
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
//		TextView textView = getGenericView();
//		textView.setText(getChild(groupPosition, childPosition).toString());
//		textView.setTextColor(R.color.darkBlue);
//		return textView;

		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.child_layout, null);
		}
		TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
		tv.setText(getChild(groupPosition, childPosition).toString());
		return convertView;

	}

	@Override
	public int getGroupCount() {
		return groups.length;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groups[groupPosition];
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	// Return a group view. You can load your custom layout here.
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
//		TextView textView = getGenericView();
//		textView.setText(getGroup(groupPosition).toString());
//		textView.setTextColor(R.color.red);
//		return textView;
		
	      String group = getGroup(groupPosition).toString();
	        if (convertView == null) {
	            LayoutInflater infalInflater = (LayoutInflater) context
	                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            convertView = infalInflater.inflate(R.layout.group_layout, null);
	        }
	        TextView tv = (TextView) convertView.findViewById(R.id.welcomearea_tvGroup);
	        tv.setText(group);
	        return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}

	public TextView getGenericView() {
		// Layout parameters for the ExpandableListView
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT, 64);

		TextView textView = new TextView(this.context);
		textView.setLayoutParams(lp);
		// Center the text vertically
		textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		textView.setTextColor(R.color.green);
		// Set the text starting position
		textView.setPadding(36, 0, 0, 0);
		return textView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		int i = 0;
		try {
			i = children[groupPosition].length;

		} catch (Exception e) {
		}

		return i;
	}
}
